package com.lm.activiti.boot.tools.excel;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.google.common.collect.Lists;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author lm_jcf
 * @date 2020/1/6 14:09
 */
public class EasyexcelTest {

    @Test
    public void buildData() throws FileNotFoundException {

        List<List<String>> headList = Lists.newArrayList();
        List<List<String>> contentList = Lists.newArrayList();

        List<String> row_1 = Lists.newArrayList("吴立媛", "G123464521", "女", "高二（7）班", "公假", "06-28  周一  第8节\n" + "06-29  周二  第8节");
//        List<String> row_2 = Lists.newArrayList("张雯雯", "G123464522", "女", "高二（7）班", "公假", "06-28  周一  第8节\n" + "06-29  周二  第8节");
//        List<String> row_3 = Lists.newArrayList("黄丽", "G123464523", "女", "高二（7）班", "公假", "06-28  周一  第8节\n" + "06-29  周二  第8节");
        List<String> row_2 = Lists.newArrayList("张雯雯", "G123464522", "女", "高二（7）班");
        List<String> row_3 = Lists.newArrayList("黄丽", "G123464523", "女", "高二（7）班");

        contentList.add(row_1);
        contentList.add(row_2);
        contentList.add(row_3);

        this.createFile(headList, contentList);
    }

    public void createFile(List<List<String>> headList, List<List<String>> contentList) throws FileNotFoundException {
        String filePath = "C:\\Users\\lm_jcf\\Desktop\\";

        File file = new File(filePath + DateTime.now().getSecondOfDay() + ".xlsx");
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }

        System.out.println(file.getName());
        System.out.println(file.getPath());

        ExcelWriter writer = EasyExcelFactory.getWriter(new FileOutputStream(file));
        writer.write0(contentList, new com.alibaba.excel.metadata.Sheet(1, 0, null, "第一页", headList));

        writer.merge(1, 3, 5, 5);
        writer.finish();
    }
}
